package cn.sunline.pub.sftp;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Vector;


import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

/**
 * SFTP实用类
 * 
 */
public class SFTPFileTransfer {
	/*private static final Logger logger = Logger
			.getLogger(SFTPFileTransfer.class);*/

	public ChannelSftp connect(String host, int port, String username,
			String password) throws Exception {
		ChannelSftp sftp = null;
		JSch jsch = new JSch();
		Session sshSession = null;

		sshSession = jsch.getSession(username, host, port);

		sshSession.setPassword(password);
		Properties sshConfig = new Properties();
		sshConfig.put("StrictHostKeyChecking", "no");
		sshSession.setConfig(sshConfig);
		try {
			sshSession.connect();
		} catch (JSchException e) {
			e.printStackTrace();
			throw new Exception("ip,密码或用户名错误");
		}

		Channel channel = sshSession.openChannel("sftp");
		channel.connect();
		sftp = (ChannelSftp) channel;

		return sftp;
	}

	/**
	 * 上传文件
	 * 
	 * @param directory
	 *            上传的目录
	 * @param uploadFile
	 *            要上传的文件
	 * @param sftp
	 * @throws Exception
	 */
	public void upload(String directory, String uploadFile, ChannelSftp sftp)
			throws Exception {
		try {
			sftp.cd(directory);
		} catch (SftpException e) {
			// TODO Auto-generated catch block
			throw new Exception("上传目录找不到");
		}
		File file = new File(uploadFile);
		FileInputStream fileIn = new FileInputStream(file);
		sftp.put(fileIn, file.getName());
		fileIn.close();

	}

	/**
	 * 下载文件
	 * 
	 * @param directory
	 *            下载目录
	 * @param downloadFile
	 *            下载的文件
	 * @param saveFile
	 *            存在本地的路径
	 * @param sftp
	 * @throws SftpException
	 * @throws IOException
	 */
	public void download(String directory, String downloadFile,
			String saveFile, ChannelSftp sftp) throws SftpException,
			IOException {
		sftp.cd(directory);
		File file = new File(saveFile);
		FileOutputStream fileout = new FileOutputStream(file);
		sftp.get(downloadFile, fileout);

		fileout.close();

	}
	
	

	/**
	 * 删除文件
	 * 
	 * @param directory
	 *            要删除文件所在目录
	 * @param deleteFile
	 *            要删除的文件
	 * @param sftp
	 */
	public void delete(String directory, String deleteFile, ChannelSftp sftp) {
		try {
			sftp.cd(directory);
			sftp.rm(deleteFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 列出目录下的文件
	 * 
	 * @param directory
	 *            要列出的目录
	 * @param sftp
	 * @return
	 * @throws SftpException
	 */
	public Vector listFiles(String directory, ChannelSftp sftp)
			throws SftpException {
		return sftp.ls(directory);
	}

	public void exit(ChannelSftp sftp) throws SftpException, JSchException {
		sftp.disconnect();
		sftp.exit();
		sftp.getSession().disconnect();
	}

	public static void main(String[] args) throws Exception {
		String data = "xingnengindex.txt";

		SFTPFileTransfer sf = new SFTPFileTransfer();
		String host = "10.22.10.34";
		int port = 22;
		String username = "test";
		String password = "test";
		String directory = "/home/test/tmp";
		String uploadFile = "C:/Users/zl117/Desktop/test/xingnengindex.txt";
		String downloadFile = data;
		String saveFile = "C:/Users/zl117/Desktop/test/1" + data;
		String deleteFile = data;
		ChannelSftp sftp = sf.connect(host, port, username, password);
		sf.upload(directory, uploadFile, sftp);
		sf.download(directory, downloadFile, saveFile, sftp);
		sf.delete(directory, deleteFile, sftp);
		try {
			// sftp.cd(directory);
			// sftp.mkdir("ss");
			// Thread.sleep(5000);
			sf.exit(sftp);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
